//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.entity.Purchase;
import com.qst.crop.entity.PurchaseDetail;
import com.qst.crop.entity.SellPurchase;
import com.qst.crop.entity.Shoppingcart;
import com.qst.crop.model.ShoppingModel;
import com.qst.crop.service.PurchaseDetailService;
import com.qst.crop.service.PurchaseService;
import com.qst.crop.service.SellPurchaseService;
import com.qst.crop.service.ShoppingcartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping({"/cart"})
@Api(
        tags = {"购物车接口"}
)
public class CartController {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private ShoppingcartService shoppingcartService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private SellPurchaseService sellPurchaseService;

    public CartController() {
    }

    @ApiOperation("添加商品到购物车")
    @PostMapping({"/add/{id}"})
    public Result add(@PathVariable("id") Integer id) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Shoppingcart shoppingcart = new Shoppingcart();
        shoppingcart.setOwnName(name);
        shoppingcart.setOrderId(id);
        shoppingcart.setCount(1);
        shoppingcart.setCreateTime(new Date());
        shoppingcart.setUpdateTime(new Date());
        List<ShoppingModel> shoppingcarts = this.shoppingcartService.selectByUserOrderId(id);
        if (null != shoppingcarts && shoppingcarts.size() > 0) {
            shoppingcart.setCount(((ShoppingModel)shoppingcarts.get(0)).getCount() + 1);
            shoppingcart.setShoppingId(((ShoppingModel)shoppingcarts.get(0)).getShoppingId());
            this.shoppingcartService.update(shoppingcart);
        } else {
            this.shoppingcartService.add(shoppingcart);
        }

        return new Result(true, 20000, "添加商品到购物车成功");
    }

    @ApiOperation("从购物车删除商品")
    @DeleteMapping({"/delete/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        this.shoppingcartService.delete(id);
        return new Result(true, 20000, "删除商品成功");
    }

    @ApiOperation("展示购物车列表")
    @GetMapping({"/show"})
    public Result<List<ShoppingModel>> show() {
        List<ShoppingModel> shoppingModel = this.shoppingcartService.selectByUsername();
        return new Result(true, 20000, "查询成功", shoppingModel);
    }

    @ApiOperation("更新购物车数量")
    @PutMapping({"/update/{id}/{count}"})
    public Result update(@PathVariable("id") Integer id, @PathVariable("count") Integer count) {
        Shoppingcart shoppingcart = new Shoppingcart();
        shoppingcart.setShoppingId(id);
        shoppingcart.setCount(count);
        shoppingcart.setUpdateTime(new Date());
        this.shoppingcartService.update(shoppingcart);
        return new Result(true, 20000, "更新商品数量成功");
    }

    @ApiOperation("提交订单")
    @PostMapping({"/commitOrder/{addId}/{tMoney}"})
    public Result commitOrder(@PathVariable("addId") Integer addId, @PathVariable("tMoney") String tMoney, @RequestBody List<ShoppingModel> shoppingModelList) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Purchase purchase = new Purchase();
        purchase.setPurchaseType(1);
        purchase.setOwnName(name);
        purchase.setAddress(addId.toString());
        purchase.setPurchaseStatus(1);
        purchase.setTotalPrice(new BigDecimal(tMoney));
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        this.purchaseService.add(purchase);
        Purchase PurchaseGetId = this.purchaseService.selectNewPurchaseId(name);
        Integer purchaseId = PurchaseGetId.getPurchaseId();
        if (null != shoppingModelList && shoppingModelList.size() > 0) {
            PurchaseDetail purchaseDetail = new PurchaseDetail();
            SellPurchase sellPurchase = new SellPurchase();

            for(int i = 0; i < shoppingModelList.size(); ++i) {
                ShoppingModel shoppingModel = (ShoppingModel)shoppingModelList.get(i);
                purchaseDetail.setCount(shoppingModel.getCount());
                purchaseDetail.setOrderId(shoppingModel.getOrderId());
                purchaseDetail.setPurchaseId(purchaseId);
                purchaseDetail.setSumPrice(new BigDecimal(Double.parseDouble(shoppingModel.getPrice()) * (double)shoppingModel.getCount()));
                purchaseDetail.setUninPrice(new BigDecimal(shoppingModel.getPrice()));
                this.purchaseDetailService.add(purchaseDetail);
                sellPurchase.setOrderId(shoppingModel.getOrderId());
                sellPurchase.setAddress(addId.toString());
                sellPurchase.setOwnName(shoppingModel.getOwnName());
                sellPurchase.setPurchaseId(purchaseId);
                sellPurchase.setPurchaseStatus(1);
                sellPurchase.setPurchaseType(2);
                sellPurchase.setSumPrice(new BigDecimal(Double.parseDouble(shoppingModel.getPrice()) * (double)shoppingModel.getCount()));
                sellPurchase.setUninPricee(new BigDecimal(shoppingModel.getPrice()));
                sellPurchase.setCreateTime(new Date());
                sellPurchase.setUpdateTime(new Date());
                this.sellPurchaseService.add(sellPurchase);
                this.shoppingcartService.delete(shoppingModel.getShoppingId());
            }
        }

        return new Result(true, 20000, "提交成功");
    }
}
